/*
 * Contact service object based on com.nokia.device.contacts Interface
 */
var contact = null;

/*
 * Initializes contact service object
 */
function initService(){
	try {
		contact = nokia.device.load("contacts");
	}catch(e){
		alert(e.toString());
	}
}

/*
 * Starts the device native contact editor 
 */
function startEditor(){
    try {
        contact.startEditor(startEditorCallBack, null, startEditorCallBackErr);
    } 
    catch (e) {
        alert(e.toString());
    }
}

/*
 * Callback handler for startEditor
 */
function startEditorCallBack(){
    //Do something
}

/*
 * Error Callback handler for startEditor
 */
function startEditorCallBackErr(err){
    alert(err);
}

/*
 * Gets a list of contacts matching the pattern provided in contactFilter field
 */
function listContacts(){
    var key = document.getElementById("contactFilter").value;
    
    try {
      contact.getContacts(getContactsCallBack, key, contact.SORT_ASCENDING, getContactsCallBackErr);
    } 
    catch (e) {
        alert("listContacts: " + e.toString());
    }
}

/*
 * getContacts callback function that handles contact iterator and puts content to contactList container
 */
function getContactsCallBack(outPut){
    var entry = null;
    var content = "<table border='0' width='100%'><tr><th width='5'><input type='checkbox' name='chAll' onClick='chALL(this.checked)'/></th><th colspan='2'><ul class='button-rounded'><li><a href='#' onclick='deleteContacts();'>Delete selected</a></li></th></tr>";
    try {
        while ((entry = outPut.next()) != null) {
            content += showContact(entry);
        }
    } 
    catch (e) {
        alert("getContactsCallBack: " + e.toString());
    }
    content += "</table>";
    document.getElementById("contactList").innerHTML = content;
}

/*
 * Error Callback handler for getContacts
 */
function getContactsCallBackErr(err){
    	
	if(err.code == 101){ //data not found, no entries on given filter.
		document.getElementById("contactList").innerHTML = "<table border='0' width='100%'><tr><th width='5'><input type='checkbox' name='chAll'/></th><th colspan='2'><ul class='button-rounded'><li><a href='#'>Delete selected</a></li></th></tr></table><p>No Contacts</p>";
	}else{
	    alert(err.toString());
	}
}

/*
 * Deletes contact by specified id
 * id - unique identifier of contact
 */
function deleteContact(id){
    try {
        contact.deleteContacts(id);
    } 
    catch (e) {
        alert(e.toString());
    }
}

/*
 * Adds a new contact based on the data in contact form
 */
function addContact(){
    try {
        var contactEntry = prepareContactEntry();
        contact.addContact(contactEntry);
    } 
    catch (e) {
        alert(e.toString());
    }
}

/*
 * Updates existing contact by specified id
 *  id - unique identifier of contact
 */

function updateContact(id){
    try {
        var contactUpdate = prepareContactEntry();
        contactUpdate.id = id;
        contact.updateContact(contactUpdate);
    } 
    catch (e) {
        alert(e.toString());
    }
}

/*
 * Gets a contact by specified id 
 * id - unique identifier of contact
 */

function getContact(id){
    var result = null;
    try {
        result = contact.getContactInfo(id);
    } 
    catch (e) {
        alert(e.toString());
    }
    return result;
}



